Skip to content

Go: Fix multiple bugs in rewrite-go module#7212

Merged
jkschneider merged 1 commit intomainfrom
turbo-hawk-2
Mar 31, 2026
Merged

Go: Fix multiple bugs in rewrite-go module#7212
jkschneider merged 1 commit intomainfrom
turbo-hawk-2

Conversation

@knutwannheden
Copy link
Copy Markdown
Contributor

Motivation

Code review of the rewrite-go module identified several bugs and missing visitor coverage that would impact correctness and robustness of the Go language support.

Summary

  • Fix panic recovery in RPC serversafeHandleRequest now returns a proper JSON-RPC error response instead of nil when a panic is caught, preventing silent session death
  • Fix parser to handle multiple import blocks — Go allows import "fmt" followed by import "os" (common in generated code). The parser previously only processed the first import block, corrupting cursor position tracking for the rest of the file. A new ImportBlock marker tracks block boundaries, with corresponding printer, RPC serialization, and Java marker support.
  • Fix visitAndCast/visitExpression nil panics — These helpers now handle nil returns from visitors instead of panicking on the type assertion
  • Fix VisitCompilationUnit to visit PackageDecl and Imports — These were previously skipped entirely, meaning no recipe visitor could transform package names or imports
  • Remove dead code in mapArrayType — Unreachable closePrefix computation that was immediately overwritten

Test plan

  • Added TestParseMultipleImportBlocks — parse-print round-trip for import "fmt" / import "os"
  • Added TestParseMultipleGroupedImportBlocks — parse-print round-trip for multiple import (...) blocks
  • Added TestVisitorReturningNilDoesNotPanic — visitor returning nil for a Return node doesn't crash
  • Added TestVisitorVisitsImports — confirms import nodes are visited (was 0, now 2)
  • Added TestVisitorVisitsPackageDecl — confirms package decl identifier is visited
  • All existing Go tests pass
  • Java compilation passes

Motivation: Code review of the rewrite-go module identified several bugs
and missing visitor coverage that would impact correctness and robustness.

Summary:
- Fix panic recovery in safeHandleRequest to return a proper JSON-RPC
  error response instead of nil when a panic is caught
- Fix parser to handle multiple import blocks (e.g., `import "fmt"` followed
  by `import "os"`), using a new ImportBlock marker to track block boundaries
- Fix visitAndCast and visitExpression to handle nil returns from visitors
  instead of panicking on the type assertion
- Fix VisitCompilationUnit to visit PackageDecl and Imports, which were
  previously skipped entirely (preventing recipe visitors from transforming
  package names or imports)
- Remove dead code in mapArrayType (unreachable closePrefix computation)
@github-project-automation github-project-automation Bot moved this to In Progress in OpenRewrite Mar 31, 2026
@knutwannheden knutwannheden changed the title Fix multiple bugs in rewrite-go module Go: Fix multiple bugs in rewrite-go module Mar 31, 2026
@jkschneider jkschneider merged commit 2e883d4 into main Mar 31, 2026
1 check passed
@jkschneider jkschneider deleted the turbo-hawk-2 branch March 31, 2026 11:56
@github-project-automation github-project-automation Bot moved this from In Progress to Done in OpenRewrite Mar 31, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

Archived in project

Development

Successfully merging this pull request may close these issues.

2 participants